local DB = require("common.DB")
local LuaMongo = _G.lua_mongo
local Config = require("Config")
local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local YunYingLogic = require("yunying.YunYingLogic")
local OpenAct = require("present.OpenAct")

local DBUpdate = {_id=nil}								-- 更新	
local DBUpdateField = {}								-- 更新域


KEY_SERVER_START_TIME = "serverStartTime"				-- 开服时间
KEY_IDENTITY_MAX = "identityMax"						-- 自增的最大的数字 从1开始
KEY_UNION_IDENTITY_MAX = "unionIdentityMax"				-- 自增的最大的数字 从1开始
KEY_REWARD_INDEX = "rewardIndex"						-- 好友排行榜奖励索引rewardIndex
KEY_BAN_IP = "banIp"									-- 被禁IP
KEY_CHONGJI_ACT = "chongJiAct"							-- 全服冲级活动 领取情况
KEY_ACT_MONTH = "actMonth"								-- 当前计算的真实月份
KEY_UNION_WAR = "unionWar"								-- 公会战
KEY_ACT_MONTH_LIST = "actMonthList"						-- 现实活动
KEY_RESET_TIME = "resetTime"
KEY_DRILL = "drill"										-- 过关斩将信息（本期开始时间等）
KEY_SHARE_PLATFORM_BOARD = "sharePlatformBoard"			-- 分享排行
KEY_BILLBOARD_AIM = "billboardAim"						-- 排行榜达标记录
KEY_JJC_LADDER = "jjcLadder"							-- 天梯争霸信息
KEY_VALLEY = "valley"									-- 荣耀峡谷信息
KEY_LIANYU = "lianyu" 									-- 绝望深渊信息
KEY_MIDDLE_INFO = "middleInfo"							-- 跨服链接信息
KEY_CHAMPION_END_TIME = "championEndTime"				-- 冠军竞技场本赛季结束时间
KEY_DRAGON_NEST_CITIAO = "dragonNestCiTiao"             -- 冰龙巢穴词条
KEY_WORK_MAN = "workMan"                                -- 打工人活动
KEY_OPEN_SERVER_TOPUP_TOP = "openServerTopupTop"		-- 开服充值排名活动
KEY_CIYUAN_MOZHU = "ciyuanMoZhu"                        -- 次元魔蛛
KEY_YJ_TIME = "yjTreasureTime"                          -- 遗迹探险时间

db = db or {
	serverStartTime = nil,								-- 开服时间
	zhuanpanRecord = nil,								-- 转盘中奖记录
	identityMax = nil,									-- 自增的最大的数字 从1开始
    rewardIndex = nil,                                  -- 好友排行榜奖励索引rewardIndex
    unionIdentityMax = nil,                             -- 自增的最大的数字 从1开始
    mergeSvrIndex = nil,								-- 合服之后对应的服务器索引 比如如果10001,10003,100004合服了 这里的数据结构是{10001,10003,10004}
    banIp = {},                                         -- 被禁IP
	chongJiAct  = {},                                   -- 全服冲级活动 领取情况
	actMonth = nil,                                     -- 当前计算的真实月份
	unionWarState = nil,                                -- 当前工会战状态
	actMonthList = nil,                                 -- 现实活动
	resetTime = nil,                                    -- 确认是否重置数据
	middleInfo = {},									-- 跨服链接信息
	championEndTime = nil,							    -- 冠军竞技场本赛季结束时间
	dragonNestCiTiao = {},                              -- 冰龙巢穴词条
	workMan = {},                                       -- 打工人活动
	ciyuanMoZhu = nil,                                  -- 次元魔蛛 
    yjTreasureTime = nil,                               -- 遗迹探险时间 
}

function init()
	LuaMongo.find(DB.db_common)
	if not LuaMongo.next(db) then
		LuaMongo.insert(DB.db_common, db)
	end
end

-- 修改db单个字段九 零 一 起玩 www .9 01 7  5.com 
function updateValue(key, value)
	if not key then return end	
	if not db then return end
	if value then
		DBUpdateField["$set"] = {[key]=value}
		DBUpdateField["$unset"] = nil
	else
		DBUpdateField["$set"] = nil
		DBUpdateField["$unset"] = {[key]=1}
	end
	DBUpdate._id = db._id
	db[key] = value
	LuaMongo.update(DB.db_common, DBUpdate, DBUpdateField)
end

function getValueByKey(key)
	if not db then return end
	return db[key]
end

-- 同步db内容
function reset()
	LuaMongo.find(DB.db_common)
	
	if not LuaMongo.next(db) then
		db = {banIp = {},}
	end
	print("db.serverStartTime:",db.serverStartTime)
end

-- 设置开服时间
function setServerOpenTime(time)
	updateValue(KEY_SERVER_START_TIME, time)

    YunYingLogic.onServerOpenTime()
	OpenAct.onServerOpenTime()
end

function getServerOpenTime()
	return db.serverStartTime or 0
end

-- 获得当前是开服第几天（如果是开服第一天返回1）
function getServerOpenDay()
	local openTime = getServerOpenTime()
	if openTime == 0 then
		return
	end
	
	local startTime = Util.getDayStartTime(openTime)
	
	local now = os.time()
	return math.ceil((now - startTime + 1) / 24 / 3600)
end

function getIdentityMax()
	if db.identityMax == nil then
		db.identityMax = math.random(10001, 20000)	
		setIdentityMax(db.identityMax)
	elseif db.identityMax < 10001 then
		db.identityMax = math.random(10001, 20000) -- 兼容老规则下数据
		setIdentityMax(db.identityMax)
	end
	
	return db.identityMax
end

-- 虚拟ID，假的
function getFakeIdentityMax()
	return math.random(1, 9999)
end

function setIdentityMax(idMax)
	updateValue(KEY_IDENTITY_MAX, idMax)
end

function getUnionIdentityMax()
	if db.unionIdentityMax == nil then
		db.unionIdentityMax = math.random(10001, 20000)	
		setUnionIdentityMax(db.unionIdentityMax)
	elseif db.unionIdentityMax < 10001 then
		db.unionIdentityMax = math.random(10001, 20000) -- 兼容老规则下数据
		setUnionIdentityMax(db.unionIdentityMax)
	end
	
	return db.unionIdentityMax
end

function setUnionIdentityMax(idMax)
	updateValue(KEY_UNION_IDENTITY_MAX, idMax)
end

function getRewardIndex()
	return db.rewardIndex or 0
end

function setRewardIndex(rewardIndex)
	updateValue(KEY_REWARD_INDEX, rewardIndex)
end

function getMergeSvrIndex()
	return db.mergeSvrIndex
end

cnt = 1
function setBanIp(ip,time)
    db.banIp[cnt] =db.banIp[cnt] or {}
    db.banIp[cnt].ip = ip
    db.banIp[cnt].time = time
    cnt = cnt + 1
	
	updateValue(KEY_BAN_IP, db.banIp)
end

function getBanIp(ip)
    for k,v in pairs(db.banIp) do
        if v.ip == ip then
            return v.ip,v.time
        end
    end
    return 
end

function delBanIp(ip)
    for k,v in pairs(db.banIp) do
        if v.ip == ip then
            db.banIp[k] = nil
        end
    end

    updateValue(KEY_BAN_IP, db.banIp)
end

-- 全服创号 冲级活动
function getSvrChongJiGet(id)
	local num = 0 
	if not db.chongJiAct then
		return num
	end

	if db.chongJiAct[id] then
		num = db.chongJiAct[id] or 0
	end

	return num
end

function addSvrChongJiGet(id)
	db.chongJiAct = db.chongJiAct or {}
    db.chongJiAct[id] = db.chongJiAct[id] or 0
    db.chongJiAct[id] = db.chongJiAct[id] + 1

	updateValue(KEY_CHONGJI_ACT, db.chongJiAct)	
end

function getActMonth()
    return db.actMonth or 0
end

function setActMonth(month)
	updateValue(KEY_ACT_MONTH, month)	
end

function getActMonthList(id)
	local act = nil 
	if not db.actMonthList then
		return act
	end

	if db.actMonthList[id] then
		act = db.actMonthList[id] or 0
	end

	return act
end

function setActMonthList(id, endTime, month)
	db.actMonthList = db.actMonthList or {}
    db.actMonthList[id] = db.actMonthList[id] or {}
    db.actMonthList[id].endTime = endTime
	db.actMonthList[id].month = month
	updateValue(KEY_ACT_MONTH_LIST, db.actMonthList)
end

function getResetTime()
	return db.resetTime or 0
end

function setResetTime(time)
	updateValue(KEY_RESET_TIME, time)
end

function getMiddleInfo()
	return db.middleInfo
end

function setMiddleInfo(ip, port, host)
	db.middleInfo.ip = ip
	db.middleInfo.port = port
	db.middleInfo.host = host
	updateValue(KEY_MIDDLE_INFO, db.middleInfo)
end

function getChampionEndTime()
	return db.championEndTime or 0
end

function setChampionEndTime(time)
	updateValue(KEY_CHAMPION_END_TIME, time)
end

function getDragonNestCitiao()
	return db.dragonNestCiTiao
end

function setDragonNestCitiao(list, time)
	db.dragonNestCiTiao.citiao = Util.copyTable(list)
	db.dragonNestCiTiao.time = time
	updateValue(KEY_DRAGON_NEST_CITIAO, db.dragonNestCiTiao)
end

function setWorkManState(state)
    db.workMan.state = state
    updateValue(KEY_WORK_MAN, db.workMan)
end

function setWorkManGroup(group)
    db.workMan.group = group
    updateValue(KEY_WORK_MAN, db.workMan)
end

function setWorkManTs(ts)
    db.workMan.time = ts
    updateValue(KEY_WORK_MAN, db.workMan)
end

function getWorkMan()
	return db.workMan
end

-- 获取合服时间
function getMergeServerTime()
	return db.mergeServerTime or 0
end


-- 获得当前是合服第几天（如果是合服第一天返回1）
function getServerMergeDay()
	local mergeTime = getMergeServerTime()
	if mergeTime == 0 then
		return
	end
	
	local startTime = Util.getDayStartTime(mergeTime)
	
	local now = os.time()
	return math.ceil((now - startTime + 1) / 24 / 3600)
end

function setMergeTime(ts)
    db.mergeServerTime = ts
    updateValue("mergeServerTime", db.mergeServerTime)
end

function getYjTreasureEndTime()
	return db.yjTreasureTime or 0
end

function setYjTreasureEndTime(time)
	updateValue(KEY_YJ_TIME, time)
end